Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple script vm #407

Merged
merged 10 commits into from
Aug 15, 2024
Merged

Simple script vm #407

merged 10 commits into from
Aug 15, 2024

Conversation

polarker
Copy link
Member

A symbolic execution vm for tx scripts. Most of the TxScript instructions are implemented, we could add more based on feedback.

Not many unit tests for now as I don't have much time. We could add them in the future.

@polarker polarker requested review from h0ngcha0 and Lbqds August 13, 2024 13:03
* - undefined if no tokens are approved
* - 'unknown' if the tokens cannot be determined
* - an array of tokens if the tokens are known
*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have better types for this?

break
}
default:
unimplemented(instr.name)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instructions that are not used much in TxScript are not implemented for now.

break
case 'Dup':
const val = operandStack.pop()
operandStack.push(val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to push the val twice for the Dup instr.

type SimulatorVar<K extends string, V> = { kind: K; value: V } | { kind: `Symbol-${K}`; value: undefined }

function unaryOp<K extends string, V>(x: SimulatorVar<K, V>, op: (x: V) => V): SimulatorVar<K, V> {
if (x.kind.startsWith('symbol')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to use Symbol instead of symbol in these *Op functions? It seems that 'Symbol-Bool'.startsWith('symbol') returns false.

expect(getCalls1[0].approvedTokens).toBe(undefined)
})

it.only('should use transact methods for token', async function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to remove the only here?

expect(deployCalls2.length).toBe(0)
})

it.only('should test multicall', async function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

if (spender.kind.startsWith('Symbol')) {
approved.setUnknown() // The spender might be the caller
} else if (spender === callerAddress) {
approved.addApprovedAttoAlph(amount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we set the approved to unknown as well if callerAddress isn't spender?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only show the approvals for call address in the return, so we don't need to care about other addresses.

Copy link
Member

@h0ngcha0 h0ngcha0 Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking about perhaps this would underestimate the total amount of approvals in corner cases, since we could have something like

foo.call{callerAddress!() -> 1 alph, alice -> 2 alph)(...)

where alice is actually the same as callerAddress!(). Could this happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If alice's address is unknown, then branch spender.kind.startsWith('Symbol') will be executed, the approved assets would be set to unknown.

If alice's address is known, then we will compare if the address is equal to caller address.

@polarker polarker merged commit a3ebfcf into master Aug 15, 2024
6 of 7 checks passed
@polarker
Copy link
Member Author

@Lbqds @h0ngcha0 I have resolved all comments and will release a new version. If you have more comments, please feel free to add them here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants